package android.medicalcloud.com.yiapp.yilian.ui.fragment;

import android.medicalcloud.com.yiapp.R;
import android.medicalcloud.com.yiapp.base.fragment.BaseFragment;
import android.medicalcloud.com.yiapp.base.net.lisener.ViewNetCallBack;
import android.medicalcloud.com.yiapp.base.utils.Logger;
import android.medicalcloud.com.yiapp.base.utils.StringTools;
import android.medicalcloud.com.yiapp.yilian.application.YilianAppcation;
import android.medicalcloud.com.yiapp.yilian.configer.enums.HttpConfig;
import android.medicalcloud.com.yiapp.yilian.logic.CommonController;
import android.medicalcloud.com.yiapp.yilian.logic.PushController;
import android.medicalcloud.com.yiapp.yilian.logic.UserController;
import android.medicalcloud.com.yiapp.yilian.model.UserModel;
import android.medicalcloud.com.yiapp.yilian.ui.view.dialog.SureDialog;
import android.medicalcloud.com.yiapp.yilian.utils.android.GsonTool;
import android.medicalcloud.com.yiapp.yilian.utils.android.ImageLoader;
import android.medicalcloud.com.yiapp.yilian.utils.android.IntentTool;
import android.medicalcloud.com.yiapp.yilian.utils.android.JsonTool;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.tencent.openqq.protocol.imsdk.im_common;

import java.io.Serializable;

/**
 * Created by wanggenlin on 17-5-18.
 */
public class MeFragment extends BaseFragment implements ViewNetCallBack {

    ImageView avatarIv;
    TextView nameTv,hospitalTv,incomeTv;
    View loginOutLayout;
    SureDialog sureDialog;
    @Override
    protected void initView(View view) {
        view.findViewById(R.id.meLayout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentTool.person(getActivity());
            }
        });
        view.findViewById(R.id.modifyPassLayout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentTool.updatePassword(getActivity());
            }
        });
        view.findViewById(R.id.mobileLayout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CommonController.getInstance().getConfigModel()==null||
                        StringTools.isNullOrEmpty(CommonController.getInstance().getConfigModel().getServiceTel())){
                    return;
                }
                IntentTool.call(getActivity(),CommonController.getInstance().getConfigModel().getServiceTel());
            }
        });

        avatarIv =(ImageView) view.findViewById(R.id.avatarIv);
        nameTv =(TextView) view.findViewById(R.id.nameTv);
        hospitalTv =(TextView) view.findViewById(R.id.hospitalTv);
        incomeTv =(TextView) view.findViewById(R.id.incomeTv);
        loginOutLayout = view.findViewById(R.id.loginOutLayout);
        loginOutLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sureDialog.show();
            }
        });
        sureDialog  = new SureDialog(getActivity());
        sureDialog.setRes("是否退出登录",false);
        sureDialog.setOnSureClickLisener(new SureDialog.OnSureClickLisener() {
            @Override
            public void onSureClick() {
                PushController.getInstance().unbind(MeFragment.this);
            }
        });
    }



    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {

    }


    @Override
    public void onResume() {
        super.onResume();
        UserController.getInstance().simpleInfor(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fra_me;
    }

    public static MeFragment newInstance() {
        MeFragment homeFragment = new MeFragment();
        return homeFragment;
    }

    @Override
    public void onConnectStart(Object o) {
    }

    @Override
    public void onConnectEnd() {
    }

    @Override
    public void onFail(Exception e, Object o, String reslut) {
    }

    @Override
    public void onData(Serializable result, int flag, boolean fromNet, Object o) {
        Logger.e("userINfor-----"+o.toString());
        if (flag== HttpConfig.loginOut.getType()){
            Logger.e("loginOut-----"+o.toString());
            UserController.getInstance().saveToken("");
            YilianAppcation.instance.clear();
            IntentTool.login(getActivity());

            return;
        }

        if (flag== HttpConfig.ubBindPush.getType()){
            UserController.getInstance().loginOut(MeFragment.this);
            return;
        }

        try {
            UserModel model = GsonTool.jsonToEntity(JsonTool.getString(o.toString(),"user"), UserModel.class);

            // ImageView avatarIv;
            //TextView nameTv,hospitalTv,incomeTv;
            if (!StringTools.isNullOrEmpty(model.getImg())){
                ImageLoader.getInstance(getActivity()).displayCileImage(model.getImg(),avatarIv);
            }
            UserController.getInstance().saveImageAvar(model.getImg());
            nameTv.setText(model.getName());
            hospitalTv.setText(model.getHospital());
            incomeTv.setText(model.getIncome()+"元");
            nameTv.setText(model.getName());

        }catch (Exception E){

        }



    }
}
